# settings.py
import os
from concurrent_log_handler import ConcurrentRotatingFileHandler


def create_logs():
    # 创建日志目录
    BASE_PATH = os.path.dirname(os.path.dirname(__file__))
    BASE_LOG_DIR = os.path.join(BASE_PATH, "logs")
    if not os.path.exists(BASE_LOG_DIR):
        os.makedirs(BASE_LOG_DIR)
    return BASE_LOG_DIR



# 创建logs目录
BASE_LOG_DIR = create_logs()
# 配置信息
LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "django": {
            "format": "[{asctime}][{levelname}][{filename}:{module}:{lineno:d}:{funcName}]：{message}",
            "datefmt": "%Y-%m-%d %H:%M:%S",
            "style": "{",
        },
        "simple": {
            "format": "{levelname} {message}",
            "datefmt": "%Y-%m-%d %H:%M:%S",
            "style": "{",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "simple",
        },
        # 记录请求处理过程中的日志
        "django-file": {
            "level": "INFO",
            "class": "concurrent_log_handler.ConcurrentRotatingFileHandler",
            "filename": os.path.join(BASE_LOG_DIR, "django.log"),
            "formatter": "django",
            "encoding": "utf8",  # 显式指定编码
            "maxBytes": 1024 * 1024 * 10,  # 文件大小10M
            "backupCount": 10,
        },
        # scheduler日志保存位置
        "scheduler-file": {
            "level": "INFO",
            "class": "concurrent_log_handler.ConcurrentRotatingFileHandler",
            "filename": os.path.join(BASE_LOG_DIR, "scheduler.log"),
            "formatter": "django",
            "encoding": "utf8",  # 显式指定编码
            "maxBytes": 1024 * 1024 * 10,  # 文件大小10M
            "backupCount": 10,
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "django-file"],  # djano请求的日志记录
            "level": "INFO",
            "propagate": True,
        },
        "default": {
            "handlers": ["django-file"],  # 请求使用的日志处理器
            "level": "INFO",
            "propagate": True,
        },
        "scheduler": {
            "handlers": ["scheduler-file"],  # 定时器使用的日志处理器
            "level": "INFO",
            "propagate": True,
        },
    },
}



"""
# settings.py下
from base_config.log_config import LOGGING
LOGGING = LOGGING
"""
